﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.DAL;

namespace QuanLyBanHang
{
    public partial class frmNhatKyHeThong : Form
    {
        public frmNhatKyHeThong()
        {
            InitializeComponent();
        }

        DataTable dt;

        private void dtpDenNgay_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                dgvNhatKy.Rows.Clear();
                string filter = string.Format("NgayTruyCap >= '{0}' and NgayTruyCap <= '{1}'", dtpTuNgay.Value,
                    dtpDenNgay.Value);
                DataRow[] r = dt.Select(filter);
                for (int i = 0; i < r.Length; i++)
                {
                    DateTime ngaytruycap = (DateTime)r[i][4];
                    dgvNhatKy.Rows.Add(r[i][0], r[i][1], r[i][2], r[i][3], ngaytruycap.ToString("dd/MM/yyyy"), ngaytruycap.ToString("HH:mm:ss"));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void txtTen_TextChanged(object sender, EventArgs e)
        {
            try
            {
                dgvNhatKy.Rows.Clear();
                string filter = string.Format("NgayTruyCap >= '{0}' and NgayTruyCap <= '{1}' and Ten like '%{2}%'", dtpTuNgay.Value,
                    dtpDenNgay.Value,txtTen.Text);
                DataRow[] r = dt.Select(filter);
                for (int i = 0; i < r.Length; i++)
                {
                    DateTime ngaytruycap = (DateTime)r[i][4];
                    dgvNhatKy.Rows.Add(r[i][0], r[i][1], r[i][2], r[i][3], ngaytruycap.ToString("dd/MM/yyyy"), ngaytruycap.ToString("HH:mm:ss"));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void frmNhatKyHeThong_Load(object sender, EventArgs e)
        {
            dt = DalNhatKyHT.XemNhatKy();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int kq = DalNhatKyHT.DeleteAll();
            this.frmNhatKyHeThong_Load(sender, e);
            this.dtpDenNgay_ValueChanged(sender, e);
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int count = dgvNhatKy.SelectedRows.Count;
            object[] arr = new object[count];
            dgvNhatKy.SelectedRows.CopyTo(arr, 0);

            for (int i = 0; i < count; i++)
            {
                DataGridViewRow r = (DataGridViewRow)dgvNhatKy.SelectedRows[i];
                int kq = DalNhatKyHT.Delete((int)r.Cells[0].Value);
            }
            this.frmNhatKyHeThong_Load(sender, e);
            this.dtpDenNgay_ValueChanged(sender, e);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return base.ProcessDialogKey(keyData);
            }
            else
                return false;
        }
    }
}
